package cn.org.timespace.easyframe.action.permission;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.easyframe.model.permission.EfControlPoint;
import cn.org.timespace.easyframe.model.permission.EfMenu;
import cn.org.timespace.easyframe.model.permission.EfRoleControlPoint;
import cn.org.timespace.easyframe.service.permission.EfControlPointService;
import cn.org.timespace.easyframe.service.permission.EfMenuService;
import cn.org.timespace.easyframe.service.permission.EfRoleControlPointService;

/**
 * <p>
 * 角色控制点
 * </p>
 * 
 * @version 1.0, 11/02/27
 * @since 1.0
 * @author 
 */

public class EfRoleControlPointAction extends EFBaseAction {

	private static final long serialVersionUID = -3416183313291298070L;

	private static Log log = LogFactory.getLog(EfRoleControlPointAction.class);

	private EfMenuService efMenuService;
	private EfControlPointService efControlPointService;
	private EfRoleControlPointService efRoleControlPointService;

	public EfRoleControlPointAction() {
		super(new EFMenuModel());
	} 

	EFMenuModel efMenuModel = (EFMenuModel) this.getModel();

	public String doList() throws Exception {

		// 查询所有的有效的菜单
		EFMenuModel t = new EFMenuModel();
		t.setDicMenuIsValid("1");
		List<EfMenu> allMenuList = efMenuService.findByCondition(t);
		getRequest().setAttribute("allMenuList", allMenuList);

		// 查询所有的有效的控制点
		List<EfControlPoint> controlPointList = efControlPointService
				.findAllByValid("1");
		getRequest().setAttribute("controlPointList", controlPointList);

		// 查询该角色关系的控制点
		String roleId = getRequest().getParameter("roleId");
		List<EfRoleControlPoint> roleControlPointList = efRoleControlPointService
				.findByCondition(roleId);
		getRequest().setAttribute("roleControlPointList", roleControlPointList);
		getRequest().setAttribute("roleControlPointListSize",
				roleControlPointList.size());

		// 将roleId传递至页面
		getRequest().setAttribute("roleId", roleId);

		return LIST;
	}

	public String doSave() throws Exception {
		String pageMsg = "保存成功";

		// 获取页面传过来的用户ID信息
		String roleId = getRequest().getParameter("roleId");
		String controlPointIds = getRequest().getParameter("controlPointIds");
		if (log.isInfoEnabled())
			log.info("角色授权[角色：" + roleId + "   选择的控制点：" + controlPointIds + "]");

		try {
			efRoleControlPointService.saveRoleControlPoint(roleId,
					controlPointIds);
		} catch (Exception e) {
			pageMsg = "保存失败！原因是：" + e.getMessage();
		}

		getRequest().setAttribute(PAGEMSG, pageMsg);
		return doList();
	}

	public EfMenuService getEfMenuService() {
		return efMenuService;
	}

	public void setEfMenuService(EfMenuService efMenuService) {
		this.efMenuService = efMenuService;
	}

	public EfControlPointService getEfControlPointService() {
		return efControlPointService;
	}

	public void setEfControlPointService(
			EfControlPointService efControlPointService) {
		this.efControlPointService = efControlPointService;
	}

	public EfRoleControlPointService getEfRoleControlPointService() {
		return efRoleControlPointService;
	}

	public void setEfRoleControlPointService(
			EfRoleControlPointService efRoleControlPointService) {
		this.efRoleControlPointService = efRoleControlPointService;
	}

}
